package com.taotao.index.service;

import java.io.IOException;
import java.util.List;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.index.mapper.ItemMapper;
import com.taotao.index.pojo.Item;
@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private SolrServer solrServer;
	@Override
	public TaotaoResult importSolr() throws IOException, SolrServerException {
		List<Item> list = itemMapper.getItem();
		
		try {
			for (Item item : list) {
				SolrInputDocument doc = new SolrInputDocument();
				doc.addField("id", item.getId());
				doc.addField("item_title", item.getTitle());
				doc.addField("item_sell_point", item.getSellPoint());
				doc.addField("item_price", item.getPrice());
				doc.addField("item_image", item.getImage());
				doc.addField("item_category_name", item.getName());
				doc.addField("item_desc", item.getDesc());
				solrServer.add(doc);
				solrServer.commit();
			}
		} catch (Exception e) {
			System.out.println("添加到solr错误");
			return TaotaoResult.build(500, e.getMessage());
		}
		return TaotaoResult.ok();

	}
	

}
